﻿<%@ WebHandler Language="C#" Class="control" %>

using System;
using System.Web;
using System.Collections.Generic;

public class control : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        HttpRequest request = context.Request;
        BlogAccountEnum type = (BlogAccountEnum)Convert.ToInt32(request.QueryString["type"]);
        int blogAccountId = Convert.ToInt32(request.QueryString["blogAccountId"]);
       string account = request.QueryString["account"];
       string des = request.QueryString["des"];
       string key = request.QueryString["accessToken"];
        DateTime expireIn = TimeUtil.ToDateTime(Convert.ToInt64(request.QueryString["expireIn"]));
        switch (type)
        {
            case BlogAccountEnum.SELECT_ALL_LIST:
                context.Response.Write(SelectList());
                break;
            case BlogAccountEnum.DELETE:
                context.Response.Write(Delete(blogAccountId));
                break;
            case BlogAccountEnum.ADD:
                context.Response.Write(Add(account, des, key, expireIn));
                break;
        }
    }

    private string Add(string account, string des, string key, DateTime expireIn)
    {
        BlogAccountProxy proxy = new BlogAccountProxy();
        BlogAccountData data = proxy.Add(account, des, key, expireIn);
        if (data != null)
        {
            return Message.CreateMessage(true, data.toJson());
        }
        else
        {
            return Message.CreateMessage(false, "帐号已存在");
        }
    }
        
    private string SelectList()
    {        
        BlogAccountProxy proxy = new BlogAccountProxy();
        List<BlogAccountData> list = proxy.SelectList();
        string result = "";
        for (int i = 0; i < list.Count; i++)
        {
            result += list[i].toJson();
            if (i < list.Count - 1)
                result += ",";
        }
        result = "[" + result + "]";
        return Message.CreateMessage(true, result);
    }

    private string Delete(int blogAccountId)
    {
        BlogAccountData data = new BlogAccountProxy().Delete(blogAccountId);
        if (data != null)
            return Message.CreateMessage(true, data.toJson());
        else
            return Message.CreateMessage(false, "微博帐号不存在或已删除");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}